package com.qiqu.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiqu.Vo.UsersFansVo;
import com.qiqu.Vo.UsersLikeArticleVo;
import com.qiqu.dao.ArticleDao;
import com.qiqu.dao.UsersDao;
import com.qiqu.dao.mapper.UsersLikeArticleCustom;
import com.qiqu.entity.Article;
import com.qiqu.entity.UsersCollection;
import com.qiqu.entity.UsersLikeArticle;
import com.qiqu.dao.UsersLikeArticleDao;
import com.qiqu.service.UsersLikeArticleService;
import com.qiqu.utils.PagedResult;
import com.qiqu.utils.TimeAgoUtils;
import org.n3r.idworker.Sid;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;


import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service("usersLikeArticleService")
public class UsersLikeArticleServiceImpl implements UsersLikeArticleService {
    @Resource
    private UsersLikeArticleDao usersLikeArticleDao;
    @Resource
    private ArticleDao articleDao;
    @Resource
    private Sid sid;
    @Resource
    private UsersDao usersDao;
    @Resource
    private UsersLikeArticleCustom usersLikeArticleCustom;


    @Override
    public boolean Like(String userId, String articleId) {
        UsersLikeArticle usersLikeArticle= new UsersLikeArticle(sid.nextShort(), userId, articleId,1,new Date());
        // 文章喜欢数量加一
        if (usersLikeArticleDao.insert(usersLikeArticle) > 0) {
            articleDao.addCount(articleId);
            //添加关注数
            Article article1=new Article();
            article1.setId(articleId);
            Article article = articleDao.selectOne(article1);
            usersDao.addReceiveLikeCounts(article.getUserId());
            return true;
        }
        return false;
    }

    @Override
    public boolean cancleLike(String userId, String articleId) {
        // 文章喜欢数量减一
        if(usersLikeArticleDao.deleteLike(userId, articleId)>0){
            articleDao.subCount(articleId);
            //添加关注数
            Article article1=new Article();
            article1.setId(articleId);
            Article article = articleDao.selectOne(article1);
            usersDao.subReceiveLikeCounts(article.getUserId());
            return true;
        }
        return false;
    }

    @Override
    public boolean isLike(String userId, String articleId) {
        Example c = new Example(UsersLikeArticle.class);
        Example.Criteria criteria = c.createCriteria();
        criteria.andEqualTo("userId",userId);
        criteria.andEqualTo("articleId",articleId);
        List<UsersLikeArticle> usersLikeArticles=usersLikeArticleDao.selectByExample(c);
        if(usersLikeArticles==null || usersLikeArticles.size()==0){
            return false;
        }
        return true;
    }

    @Override
    public int updateStatus(String userId) {

        return usersLikeArticleDao.updateStatus(userId);
    }

    @Override
    public int queryStatusCount(String userId) {
        return usersLikeArticleDao.queryStatusCount(userId).size();
    }

    @Override
    public PagedResult getLikeArticle(Integer page, Integer pageSize, String userId) {
        PageHelper.startPage(page,pageSize);
        List<UsersLikeArticleVo> usersFansVos= usersLikeArticleCustom.queryList(userId);
        for(UsersLikeArticleVo usersFansVo:usersFansVos){
            usersFansVo.setTimeDesc(TimeAgoUtils.format(usersFansVo.getCreateTime()));
        }
        PageInfo<UsersLikeArticleVo> pageList=new PageInfo<>(usersFansVos);
        PagedResult pagedResult =new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(usersFansVos);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }


}